package com.chixing.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 封装了数据库连接与关闭
 */
public class DBConn {
    private static Connection conn = null;
    public  static Connection getConnection(){
        //1. 加载数据库驱动类
        try {
            Class.forName(ReadConnProperties.getDriverName());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

//2. 连接数据（IP,port,username,password） java.sql.Connection
        //URL

        try {
            conn = DriverManager.getConnection(ReadConnProperties.getUrl(),ReadConnProperties.getUsername(),ReadConnProperties.getPassword());
            System.out.println("数据库连接成功");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return conn;
    }


    public  static void closeConnection(){
        if(conn !=null) {
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
